home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Compression / Opener / Source / utils / arc / arcadd.c < prev    next >
C/C++ Source or Header  |  1993-08-04  |  10KB  |  364 lines

  1. /*
  2.  * $Header: arcadd.c,v 1.9 88/07/31 18:45:14 hyc Exp $
  3.  */
  4.  
  5. /*
  6.  * ARC - Archive utility - ARCADD
  7.  * 
  8.  * Version 3.40, created on 06/18/86 at 13:10:18
  9.  * 
  10.  * (C) COPYRIGHT 1985,86 by System Enhancement Associates; ALL RIGHTS RESERVED
  11.  * 
  12.  * By:  Thom Henderson
  13.  * 
  14.  * Description: This file contains the routines used to add files to an archive.
  15.  * 
  16.  * Language: Computer Innovations Optimizing C86
  17.  */
  18. #include <stdio.h>
  19. #include "arc.h"
  20. #if    MTS
  21. #include <mts.h>
  22. #endif
  23.  
  24. static    void    addfile();
  25. char    *strcpy();
  26. //int    strcmp(), strlen(), free(), readhdr(), unlink();
  27. // Changed by Subrata Sircar to eliminate strlen() compiler warning
  28. int    strcmp(), free(), readhdr(), unlink();
  29. #if    UNIX
  30. int    izadir();
  31. #endif
  32. void    writehdr(), filecopy(), getstamp();
  33. void    pack(), closearc(), openarc(), abort();
  34.  
  35. void
  36. addarc(num, arg, move, update, fresh)        /* add files to archive */
  37.     int             num;    /* number of arguments */
  38.     char           *arg[];    /* pointers to arguments */
  39. int             move;        /* true if moving file */
  40. int             update;        /* true if updating */
  41. int             fresh;        /* true if freshening */
  42. {
  43.     char           *d, *dir();    /* directory junk */
  44.     char            buf[STRLEN];    /* pathname buffer */
  45.     char          **path;    /* pointer to pointers to paths */
  46.     char          **name;    /* pointer to pointers to names */
  47.     int             nfiles = 0;    /* number of files in lists */
  48.     int             notemp;    /* true until a template works */
  49.     int             nowork = 1;    /* true until files are added */
  50.     char           *i, *rindex();    /* string indexing junk */
  51.     char           *malloc(), *realloc();    /* memory allocators */
  52.     int             n;    /* index */
  53. #if    MSDOS
  54.     unsigned int    coreleft();    /* remaining memory reporter */
  55. #endif
  56.     int        addbunch();
  57.  
  58.     if (num < 1) {        /* if no files named */
  59.         num = 1;    /* then fake one */
  60. #if    DOS
  61.         arg[0] = "*.*";    /* add everything */
  62. #endif
  63. #if    UNIX
  64.         arg[0] = "*";
  65. #endif
  66. #if    MTS
  67.         arg[0] = "?";
  68. #endif
  69.     }
  70.     path = (char **) malloc(sizeof(char **));
  71.     name = (char **) malloc(sizeof(char **));
  72.  
  73.  
  74.     for (n = 0; n < num; n++) {    /* for each template supplied */
  75.         strcpy(buf, arg[n]);    /* get ready to fix path */
  76. #if    !MTS
  77.         if (!(i = rindex(buf, '\\')))
  78.             if (!(i = rindex(buf, '/')))
  79.                 if (!(i = rindex(buf, ':')))
  80.                     i = buf - 1;
  81. #else
  82.         if (!(i = rindex(buf, sepchr[0])))
  83.             if (buf[0] != tmpchr[0])
  84.                 i = buf - 1;
  85.             else
  86.                 i = buf;
  87. #endif
  88.         i++;        /* pointer to where name goes */
  89.  
  90.         notemp = 1;    /* reset files flag */
  91.         for (d = dir(arg[n]); d; d = dir(NULL)) {
  92.             notemp = 0;    /* template is giving results */
  93.             nfiles++;    /* add each matching file */
  94.             path = (char **) realloc(path, nfiles * sizeof(char **));
  95.             name = (char **) realloc(name, nfiles * sizeof(char **));
  96.             strcpy(i, d);    /* put name in path */
  97.             path[nfiles - 1] = malloc(strlen(buf) + 1);
  98.             strcpy(path[nfiles - 1], buf);
  99.             name[nfiles - 1] = d;    /* save name */
  100. #if    MSDOS
  101.             if (coreleft() < 5120) {
  102.                 nfiles = addbunch(nfiles, path, name, move, update, fresh);
  103.                 nowork = nowork && !nfiles;
  104.                 while (nfiles) {
  105.                     free(path[--nfiles]);
  106.                     free(name[nfiles]);
  107.                 }
  108.                 free(path);
  109.                 free(name);
  110.                 path = name = NULL;
  111.             }
  112. #endif
  113.         }
  114.         if (notemp && warn)
  115.             printf("No files match: %s\n", arg[n]);
  116.     }
  117.  
  118.     if (nfiles) {
  119.         nfiles = addbunch(nfiles, path, name, move, update, fresh);
  120.         nowork = nowork && !nfiles;
  121.         while (nfiles) {
  122.             free(path[--nfiles]);
  123.             free(name[nfiles]);
  124.         }
  125.         free(path);
  126.         free(name);
  127.     }
  128.     if (nowork && warn)
  129.         printf("No files were added.\n");
  130. }
  131.  
  132. int
  133. addbunch(nfiles, path, name, move, update, fresh)    /* add a bunch of files */
  134.     int             nfiles;    /* number of files to add */
  135.     char          **path;    /* pointers to pathnames */
  136.     char          **name;    /* pointers to filenames */
  137.     int             move;    /* true if moving file */
  138.     int             update;    /* true if updating */
  139.     int             fresh;    /* true if freshening */
  140. {
  141.     int             m, n;    /* indices */
  142.     char           *d;    /* swap pointer */
  143.     struct heads    hdr;    /* file header data storage */
  144.  
  145.     for (n = 0; n < nfiles - 1; n++) {    /* sort the list of names */
  146.         for (m = n + 1; m < nfiles; m++) {
  147.             if (strcmp(name[n], name[m]) > 0) {
  148.                 d = path[n];
  149.                 path[n] = path[m];
  150.                 path[m] = d;
  151.                 d = name[n];
  152.                 name[n] = name[m];
  153.                 name[m] = d;
  154.             }
  155.         }
  156.     }
  157.  
  158.     for (n = 0; n < nfiles - 1;) {    /* consolidate the list of names */
  159.         if (!strcmp(path[n], path[n + 1])    /* if duplicate names */
  160.             ||!strcmp(path[n], arcname)    /* or this archive */
  161. #if    UNIX
  162.             ||izadir(path[n])    /* or a directory */
  163. #endif
  164.             ||!strcmp(path[n], newname)    /* or the new version */
  165.             ||!strcmp(path[n], bakname)) {    /* or its backup */
  166.             free(path[n]);    /* then forget the file */
  167.             free(name[n]);
  168.             for (m = n; m < nfiles - 1; m++) {
  169.                 path[m] = path[m + 1];
  170.                 name[m] = name[m + 1];
  171.             }
  172.             nfiles--;
  173.         } else
  174.             n++;    /* else test the next one */
  175.     }
  176.  
  177.     if (!strcmp(path[n], arcname)    /* special check for last file */
  178.         ||!strcmp(path[n], newname)    /* courtesy of Rick Moore */
  179. #if    UNIX
  180.         ||izadir(path[n])
  181. #endif
  182.         || !strcmp(path[n], bakname)) {
  183.         free(path[n]);
  184.         free(name[n]);
  185.         nfiles--;
  186.     }
  187.     if (!nfiles)        /* make sure we got some */
  188.         return 0;
  189.  
  190.     for (n = 0; n < nfiles - 1; n++) {    /* watch out for duplicate
  191.                          * names */
  192.         if (!strcmp(name[n], name[n + 1]))
  193.             abort("Duplicate filenames:\n  %s\n  %s", path[n], path[n + 1]);
  194.     }
  195.     openarc(1);        /* open archive for changes */
  196.  
  197.     for (n = 0; n < nfiles; n++)    /* add each file in the list */
  198.         addfile(path[n], name[n], update, fresh);
  199.  
  200.     /* now we must copy over all files that follow our additions */
  201.  
  202.     while (readhdr(&hdr, arc)) {    /* while more entries to copy */
  203.         writehdr(&hdr, new);
  204.         filecopy(arc, new, hdr.size);
  205.     }
  206.     hdrver = 0;        /* archive EOF type */
  207.     writehdr(&hdr, new);    /* write out our end marker */
  208.     closearc(1);        /* close archive after changes */
  209.  
  210.     if (move) {        /* if this was a move */
  211.         for (n = 0; n < nfiles; n++) {    /* then delete each file
  212.                          * added */
  213.             if (unlink(path[n]) && warn) {
  214.                 printf("Cannot unsave %s\n", path[n]);
  215.                 nerrs++;
  216.             }
  217.         }
  218.     }
  219.     return nfiles;        /* say how many were added */
  220. }
  221.  
  222. static          void
  223. addfile(path, name, update, fresh)    /* add named file to archive */
  224.     char           *path;    /* path name of file to add */
  225.     char           *name;    /* name of file to add */
  226.     int             update;    /* true if updating */
  227.     int             fresh;    /* true if freshening */
  228. {
  229.     struct heads    nhdr;    /* data regarding the new file */
  230.     struct heads    ohdr;    /* data regarding an old file */
  231.     FILE           *f, *fopen();    /* file to add, opener */
  232.     long            starts, ftell();    /* file locations */
  233.     int             upd = 0;/* true if replacing an entry */
  234.  
  235. #if    !MTS
  236.     if (!(f = fopen(path, OPEN_R)))
  237. #else
  238.     if (image)
  239.         f = fopen(path, "rb");
  240.     else
  241.         f = fopen(path, "r");
  242.     if (!f)
  243. #endif
  244.     {
  245.         if (warn) {
  246.             printf("Cannot read file: %s\n", path);
  247.             nerrs++;
  248.         }
  249.         return;
  250.     }
  251. #if    !DOS
  252.     if (strlen(name) >= FNLEN) {
  253.         if (warn) {
  254.             char    buf[STRLEN];
  255.             printf("WARNING: File %s name too long!\n", name);
  256.             name[FNLEN-1]='\0';
  257.             while(1) {
  258.                 printf("  Truncate to %s (y/n)? ", name);
  259.                 fflush(stdout);
  260.                 fgets(buf, STRLEN, stdin);
  261.                 *buf = toupper(*buf);
  262.                 if (*buf == 'Y' || *buf == 'N')
  263.                     break;
  264.             }
  265.             if (*buf == 'N') {
  266.                 printf("Skipping...\n");
  267.                 fclose(f);
  268.                 return;
  269.             }
  270.         }
  271.         else {
  272.             if (note)
  273.                 printf("Skipping file: %s - name too long.\n",
  274.                     name);
  275.             fclose(f);
  276.             return;
  277.         }
  278.     }
  279. #endif
  280.     strcpy(nhdr.name, name);/* save name */
  281.     nhdr.size = 0;        /* clear out size storage */
  282.     nhdr.crc = 0;        /* clear out CRC check storage */
  283. #if    !MTS
  284.     getstamp(f, &nhdr.date, &nhdr.time);
  285. #else
  286.     {
  287.     char *buffer, *malloc();
  288.     int    inlen;
  289.     struct    GDDSECT    *region;
  290.  
  291.     region=gdinfo(f->_fd._fdub);
  292.     inlen=region->GDINLEN;
  293.     buffer=malloc(inlen);    /* maximum line length */
  294.     setbuf(f,buffer);        
  295.     f->_bufsiz=inlen;        
  296.     f->_mods|=_NOIC;    /* Don't do "$continue with" */
  297.     f->_mods&=~_IC;        /* turn it off, if set... */
  298.     }
  299.     getstamp(path, &nhdr.date, &nhdr.time);
  300. #endif
  301.  
  302.     /* position archive to spot for new file */
  303.  
  304.     if (arc) {        /* if adding to existing archive */
  305.         starts = ftell(arc);    /* where are we? */
  306.         while (readhdr(&ohdr, arc)) {    /* while more files to check */
  307.             if (!strcmp(ohdr.name, nhdr.name)) {
  308.                 upd = 1;    /* replace existing entry */
  309.                 if (update || fresh) {    /* if updating or
  310.                              * freshening */
  311.                     if (nhdr.date < ohdr.date
  312.                         || (nhdr.date == ohdr.date && nhdr.time <= ohdr.time)) {
  313.                         fseek(arc, starts, 0);
  314.                         fclose(f);
  315.                         return;    /* skip if not newer */
  316.                     }
  317.                 }
  318.             }
  319.             if (strcmp(ohdr.name, nhdr.name) >= 0)
  320.                 break;    /* found our spot */
  321.  
  322.             writehdr(&ohdr, new);    /* entry preceeds update;
  323.                          * keep it */
  324.             filecopy(arc, new, ohdr.size);
  325.             starts = ftell(arc);    /* now where are we? */
  326.         }
  327.  
  328.         if (upd) {    /* if an update */
  329.             if (note) {
  330.                 printf("Updating file: %-12s  ", name);
  331.                 fflush(stdout);
  332.             }
  333.             fseek(arc, ohdr.size, 1);
  334.         } else if (fresh) {    /* else if freshening */
  335.             fseek(arc, starts, 0);    /* then do not add files */
  336.             fclose(f);
  337.             return;
  338.         } else {    /* else adding a new file */
  339.             if (note) {
  340.                 printf("Adding file:   %-12s  ", name);
  341.                 fflush(stdout);
  342.             }
  343.             fseek(arc, starts, 0);    /* reset for next time */
  344.         }
  345.     } else {        /* no existing archive */
  346.         if (fresh) {    /* cannot freshen nothing */
  347.             fclose(f);
  348.             return;
  349.         } else if (note) {    /* else adding a file */
  350.             printf("Adding file:   %-12s  ", name);
  351.             fflush(stdout);
  352.         }
  353.     }
  354.  
  355.     starts = ftell(new);    /* note where header goes */
  356.     hdrver = ARCVER;        /* anything but end marker */
  357.     writehdr(&nhdr, new);    /* write out header skeleton */
  358.     pack(f, new, &nhdr);    /* pack file into archive */
  359.     fseek(new, starts, 0);    /* move back to header skeleton */
  360.     writehdr(&nhdr, new);    /* write out real header */
  361.     fseek(new, nhdr.size, 1);    /* skip over data to next header */
  362.     fclose(f);        /* all done with the file */
  363. }
  364.